package avicit.bdp.dcs.job.rest;

import avicit.bdp.common.dto.request.dds.ProcessSchedulerDto;
import avicit.bdp.common.dto.request.dds.ScheduleParamDto;
import avicit.bdp.common.service.dto.TaskSpeedDTO;
import avicit.bdp.dcs.job.dto.JobDTO;
import avicit.bdp.dcs.job.dto.JobDetailDto;
import avicit.bdp.dcs.job.dto.SimpleJobDto;
import avicit.bdp.dcs.job.service.DcsJobService;
import avicit.bdp.dcs.log.dto.JobLogDTO;
import avicit.bdp.dcs.semistructure.dto.SyncFileDto;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @金航数码科技有限责任公司 @作者：developer @邮箱：developer@avic-digital.com @创建时间： 2020-08-04
 * 11:39 @类说明：数据迁移、同步Job接口 @修改记录：
 */
@RestController
@RequestMapping("/api/bdp/dcs/job/JobRest")
@Api(tags = "数据迁移、同步Job接口")
public class DcsJobRest {

  @Autowired private DcsJobService jobService;

  @GetMapping("/getJobList")
  @ApiOperation(value = "查询job列表")
  public ResponseMsg<QueryRespBean<JobDTO>> getJobList(
      @ApiParam(value = "状态", name = "jobStatus") @RequestParam(required = false) Integer jobStatus,
      @ApiParam(value = "排序，按创建时间倒序：1，按创建时间升序：2，按job名称升序：3，按job名称倒序：4", name = "sort")
          @RequestParam(required = false)
          Integer sort,
      @ApiParam(value = "任务名称", name = "name") @RequestParam(required = false) String name,
      @ApiParam(value = "类型", name = "jobType", required = true) @RequestParam Integer jobType,
      @ApiParam(value = "空间ID", name = "projectId") @RequestParam(required = false)
          String projectId,
      @ApiParam(value = "当前页", name = "currentPage", required = true)
          @RequestParam(defaultValue = "1")
          Integer currentPage,
      @ApiParam(value = "每页显示条数", name = "pageSize", required = true)
          @RequestParam(defaultValue = "10")
          Integer pageSize)
      throws Exception {
    ResponseMsg<QueryRespBean<JobDTO>> responseMsg = new ResponseMsg<>();
    QueryRespBean<JobDTO> queryRespBean =
        jobService.getJobList(jobStatus, sort, name, jobType, projectId, currentPage, pageSize);
    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @PostMapping("/saveDataSource")
  @ApiOperation(value = "保存&更新数据源信息(第一步)")
  public ResponseMsg<Map> saveDataSource(@RequestBody @Valid JobDTO jobDTO) throws Exception {
    ResponseMsg<Map> responseMsg = new ResponseMsg<>();
    Map map = jobService.saveDataSource(jobDTO);
    responseMsg.setResponseBody(map);
    return responseMsg;
  }

  @PostMapping("/saveTableMapping")
  @ApiOperation(value = "保存&更新表映射关系(第二步)")
  public ResponseMsg<Boolean> saveTableMapping(@RequestBody JobDTO jobDTO) throws Exception {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    jobService.saveTableMapping(jobDTO);
    responseMsg.setResponseBody(true);
    return responseMsg;
  }

  @PostMapping("/api/debug")
  @ApiOperation(value = "调试API")
  public ResponseMsg<JSONObject> debugApi(@RequestBody JobDTO jobDTO) throws Exception {
    ResponseMsg<JSONObject> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.debugApi(jobDTO));
    return responseMsg;
  }

  @PostMapping("/updateJobStatus")
  @ApiOperation(value = "更新job状态(第四步)")
  public ResponseMsg<Boolean> updateJobStatus(@RequestBody JobDTO jobDTO) throws Exception {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    jobService.updateJobStatus(jobDTO.getId());
    return responseMsg;
  }

  @PostMapping("/releaseJob")
  @ApiOperation(value = "启用/停用job")
  public ResponseMsg<Boolean> releaseJob(@RequestBody JobDTO jobDTO) throws Exception {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.releaseJobByIds(jobDTO.getId(), jobDTO.getEnable()));
    return responseMsg;
  }

  @PostMapping("/startJobByIds")
  @ApiOperation(value = "启动job")
  public ResponseMsg<Boolean> startJobByIds(
      @ApiParam(value = "主键id(多个逗号分隔)", name = "jobIds", required = true) @RequestBody
          JobDTO jobDTO)
      throws Exception {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.startJobByIds(jobDTO.getId()));
    return responseMsg;
  }

  @PostMapping("/suspendJobByIds")
  @ApiOperation(value = "暂停job")
  public ResponseMsg<Boolean> suspendJobByIds(
      @ApiParam(value = "主键id(多个逗号分隔)", name = "jobIds", required = true) @RequestBody
          JobDTO jobDTO) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.excuteJobByIds(jobDTO.getId(), "PAUSE"));
    return responseMsg;
  }

  @PostMapping("/finishJobByIds")
  @ApiOperation(value = "结束job")
  public ResponseMsg<Boolean> finishJobByIds(
      @ApiParam(value = "主键id(多个逗号分隔)", name = "jobIds", required = true) @RequestBody
          JobDTO jobDTO) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.excuteJobByIds(jobDTO.getId(), "STOP"));
    return responseMsg;
  }

  @DeleteMapping("/deleteJobByIds")
  @ApiOperation(value = "删除job")
  public ResponseMsg<Boolean> deleteJobByIds(
      @ApiParam(value = "主键id(多个逗号分隔)", name = "jobIds", required = true) @RequestBody
          JobDTO jobDTO)
      throws Exception {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.deleteJobByIds(jobDTO.getId()));
    return responseMsg;
  }

  @PostMapping("/copyJobById")
  @ApiOperation(value = "创建类似任务")
  public ResponseMsg<Boolean> copyJobById(@RequestBody JobDTO jobDTO) throws Exception {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.copyJobById(jobDTO.getId(), jobDTO.getName()));
    return responseMsg;
  }

  @GetMapping("/getDataSourceByJobId")
  @ApiOperation(value = "查看job详情(第一步)")
  public ResponseMsg<JobDTO> getDataSourceByJobId(
      @ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId) {
    ResponseMsg<JobDTO> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.getDataSourceByJobId(jobId));
    return responseMsg;
  }

  @GetMapping("/getTableMappingByJobId")
  @ApiOperation(value = "查看job详情(第二步)")
  public ResponseMsg<Map<String, Object>> getTableMappingByJobId(
      @ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId)
      throws Exception {
    ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.getTableMappingByJobId(jobId));
    return responseMsg;
  }

  @GetMapping("/getDstTableByJobId")
  @ApiOperation(value = "查看job详情(第三步获取目标表)")
  public ResponseMsg<List<Map<String, Object>>> getDstTableByJobId(
      @ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId)
      throws Exception {
    ResponseMsg<List<Map<String, Object>>> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.getDstTableByJobId(jobId));
    return responseMsg;
  }

  @GetMapping("/getHistoryTaskList")
  @ApiOperation(value = "查询job下执行task任务列表")
  public ResponseMsg<QueryRespBean<TaskSpeedDTO>> getHistoryTaskList(
      @ApiParam(value = "流程实例id", name = "processInstanceId", required = true) @RequestParam
          String processInstanceId,
      @ApiParam(value = "当前页", name = "currentPage", required = true)
          @RequestParam(defaultValue = "1")
          Integer currentPage,
      @ApiParam(value = "每页显示条数", name = "pageSize", required = true)
          @RequestParam(defaultValue = "10")
          Integer pageSize) {

    ResponseMsg<QueryRespBean<TaskSpeedDTO>> responseMsg = new ResponseMsg<>();
    QueryRespBean<TaskSpeedDTO> queryRespBean =
        jobService.getHistoryTaskList(processInstanceId, currentPage, pageSize);
    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @GetMapping("/getHistoryJobList")
  @ApiOperation(value = "查询历史job任务")
  public ResponseMsg<QueryRespBean<JobLogDTO>> getHistoryJobList(
      @ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId,
      @ApiParam(value = "当前页", name = "currentPage", required = true)
          @RequestParam(defaultValue = "1")
          Integer currentPage,
      @ApiParam(value = "每页显示条数", name = "pageSize", required = true)
          @RequestParam(defaultValue = "10")
          Integer pageSize) {

    ResponseMsg<QueryRespBean<JobLogDTO>> responseMsg = new ResponseMsg<>();
    QueryRespBean<JobLogDTO> queryRespBean =
        jobService.getHistoryJobList(jobId, currentPage, pageSize);
    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @PostMapping("/checkTaskConfig")
  @ApiOperation(value = "检查第三步配置数据")
  public ResponseMsg<Boolean> checkTaskConfig(@RequestBody JobDTO jobDTO) throws Exception {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.checkTaskConfig(jobDTO));
    return responseMsg;
  }

  @PostMapping("/saveSchedule")
  @ApiOperation(value = "保存/修改定时任务")
  public ResponseMsg<Boolean> saveSchedule(@RequestBody ProcessSchedulerDto processSchedulerDto) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.saveSchedule(processSchedulerDto));
    return responseMsg;
  }

  @PostMapping("/previewSchedule")
  @ApiOperation(value = "预览任务调度执行时间点")
  public ResponseMsg<List<String>> previewSchedule(@RequestBody ScheduleParamDto scheduleParamDto) {
    ResponseMsg<List<String>> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.previewSchedule(scheduleParamDto));
    return responseMsg;
  }

  @GetMapping("/getScheduleByJobId")
  @ApiOperation(value = "根据jobId查看定时任务用来页面回显")
  public ResponseMsg<Map<String, Object>> getScheduleByJobId(
      @ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId) {
    ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.getScheduleByJobId(jobId));
    return responseMsg;
  }

  @GetMapping("/getDstFileByJobId")
  @ApiOperation(value = "第三步获取目标文件")
  public ResponseMsg<List<SyncFileDto>> getDstFileByJobId(
      @ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId)
      throws Exception {
    ResponseMsg<List<SyncFileDto>> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.getDstFileByJobId(jobId));
    return responseMsg;
  }

  @GetMapping("/getJobDetail")
  @ApiOperation(value = "查看job详情")
  public ResponseMsg<JobDetailDto> getJobDetail(
      @ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId)
      throws Exception {
    ResponseMsg<JobDetailDto> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.getJobDetail(jobId));
    return responseMsg;
  }

  @GetMapping("/getJobStatus")
  @ApiOperation(value = "查看job状态")
  public ResponseMsg<SimpleJobDto> getJobStatus(
      @ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId)
      throws Exception {
    ResponseMsg<SimpleJobDto> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.getJobStatus(jobId));
    return responseMsg;
  }

  @GetMapping("/resetIncrement")
  @ApiOperation(value = "重置增量信息")
  public ResponseMsg<Void> resetIncrement(
      @ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId)
      throws Exception {
    ResponseMsg<Void> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.resetIncrement(jobId));
    return responseMsg;
  }

  @GetMapping("/getJobById")
  @ApiOperation(value = "查看job详情")
  public ResponseMsg<JobDTO> getJobById(
      @ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId) {
    ResponseMsg<JobDTO> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(jobService.getJobById(jobId));
    return responseMsg;
  }
}
